package com.cola.utils.templateMessageUtils.utils;

import com.alibaba.fastjson.JSON;
import com.cola.enums.TemplateMessageType;
import com.cola.utils.PropertyUtils;
import com.cola.utils.templateMessageUtils.entity.Data;
import com.cola.utils.templateMessageUtils.entity.newOrder.NewOrderMessage;
import com.cola.utils.templateMessageUtils.entity.newOrder.NewOrderMessageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Max on 2017/8/18.
 * 有新的待配送订单通知
 */
public class WaitingSendOrderMessageUtil {
    private static final Logger logger = LoggerFactory.getLogger(WaitingSendOrderMessageUtil.class);
//    {{first.DATA}}
//    下单时间：{{keyword1.DATA}}
//    商品信息：{{keyword2.DATA}}
//    配送地址：{{keyword3.DATA}}
//    {{remark.DATA}}

    /**
     * 发送有新的待配送订单通知
     * @param orderNo
     * @param orderTime
     * @param goodsInfo
     * @param openId
     * @param address
     * @return
     */
    public static boolean send(String orderNo,String orderTime,String goodsInfo,String openId,String address){
        //获取消息实体json
        String messageJson = getMessageJson(orderTime,goodsInfo,openId,address);

        //插入待发送模板消息列表
        boolean status = TemplateCommon.insetWaitingSendMessage(orderNo, TemplateMessageType.WAITING_SEND.name(),openId,messageJson);

        if(status){
            logger.error("订单【"+orderNo+"】待配送通知已成功插入待发送队列！");
        }else{
            logger.error("新订单【"+orderNo+"】待配送通知插入待发送队列失败！");
        }
        return status;
    }

    /**
     * 获取模板消息发送请求Json
     * @param orderTime
     * @param goodsInfo
     * @param openId
     * @param address
     * @return
     */
    private static String getMessageJson(String orderTime,String goodsInfo,String openId,String address){
        NewOrderMessageData messageData = new NewOrderMessageData();
        Data data = new Data();

        data.setValue(PropertyUtils.getProperty("waiting_send_message_title"));
        messageData.setFirst(data);

        data = new Data();
        data.setValue(orderTime);
        messageData.setKeyword1(data);

        data = new Data();
        data.setValue(goodsInfo);
        messageData.setKeyword2(data);

        data = new Data();
        data.setValue(address);
        messageData.setKeyword3(data);

        data = new Data();
        data.setValue(orderTime);
        messageData.setKeyword4(data);

        data = new Data();
        data.setValue(PropertyUtils.getProperty("waiting_send_message_remark"));
        messageData.setRemark(data);

        NewOrderMessage message = new NewOrderMessage();

        message.setData(messageData);
        message.setTemplate_id(PropertyUtils.getProperty("waiting_send_template_id"));

        message.setTouser(openId);

        message.setUrl(PropertyUtils.getProperty("waiting_send_url"));

        return JSON.toJSONString(message);
    }

    public static void main(String[] args) {
        send("1111","2017-08-18","可口可乐350ml*1箱","obOPQwiBMxtetnUh5kx4DTctz2is","澳门路477号417");
    }
}
